package com.yztc.orangeentertainment.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.channalarticle.ChannalArticleVoteBean;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/13.
 */
public class ChannalArticleVoteAdapter extends RecyclerView.Adapter<ChannalArticleVoteAdapter.MyViewHolder> {
    private Context context;
    private List<ChannalArticleVoteBean> articleAttitudeBeanList;
    private int voteNum;
    private boolean flag[];
    private int a[];

    public ChannalArticleVoteAdapter(Context context,List<ChannalArticleVoteBean> articleAttitudeBeanList, int voteNum) {
        this.context = context;
        this.articleAttitudeBeanList = articleAttitudeBeanList;
        this.voteNum = voteNum;
        flag=new boolean[articleAttitudeBeanList.size()];
        a=new int[articleAttitudeBeanList.size()];
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.channalarticle_vote_item, parent,
                false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.sdArticlevoteitemimage.setImageURI(articleAttitudeBeanList.get(position).getImage());
        holder.tvArticlevoteitemname.setText(articleAttitudeBeanList.get(position).getName());
        int height = articleAttitudeBeanList.get(position).getVotenum();
        ViewGroup.LayoutParams p = holder.tvArticlevoteitemheight.getLayoutParams();
        height = (int) (70 + 260 * ((double) height / voteNum));
        p.height = height;
        holder.tvArticlevoteitemheight.setLayoutParams(p);
        holder.tvArticlevoteitemnum.setText(articleAttitudeBeanList.get(position).getVotenum() + "");
        holder.sdArticlevoteitemimage.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                if(flag[position]==false){
                    flag[position]=true;
                    holder.tvArticlevoteitemheight.setBackground(context.getDrawable(R.drawable
                            .circleshapevoteorange));
                    holder.tvArticlevoteitemnum.setText((articleAttitudeBeanList.get(position)
                            .getVotenum()+1) + "");
                    a[position]=articleAttitudeBeanList.get(position).getVotenum()+1;
                }else{
                    flag[position]=false;
                   holder.tvArticlevoteitemheight.setBackground(context.getDrawable(R.drawable
                           .circleshapevote));
                    holder.tvArticlevoteitemnum.setText((a[position]-1) + "");
                    a[position]=articleAttitudeBeanList.get(position).getVotenum();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return articleAttitudeBeanList.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.tv_articlevoteitemnum)
        TextView tvArticlevoteitemnum;
        @InjectView(R.id.tv_articlevoteitemheight)
        TextView tvArticlevoteitemheight;
        @InjectView(R.id.sd_articlevoteitemimage)
        SimpleDraweeView sdArticlevoteitemimage;
        @InjectView(R.id.tv_articlevoteitemname)
        TextView tvArticlevoteitemname;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

}
